package com.yc.supermarket.service.impl;

import com.yc.supermarket.entity.ShoppingCart;
import com.yc.supermarket.mapper.ShoppingCartMapper;
import com.yc.supermarket.service.ShoppingCartService;
import com.yc.supermarket.util.MybatisUtil;
import com.yc.supermarket.util.ResultEnum;
import com.yc.supermarket.vo.ResultVO;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCartServiceImpl implements ShoppingCartService {

    private ShoppingCartMapper mapper;
    @Override
    public Map<String,Object> finds(Map<String, Object> map) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingCartMapper.class);
        List<ShoppingCart> list =  mapper.finds(map);
        Map<String, Object> m = new HashMap<String, Object>();
        if (null == list || list.isEmpty()) {
            m.put("code", 600);
            m.put("msg", "暂无数据");
        } else {
            m.put("code", 200);
            m.put("msg", "成功");
            m.put("data", list);
        }
        MybatisUtil.close(session);
        return m;
    }

    @Override
    public ResultVO delete(ShoppingCart shoppingCart) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingCartMapper.class);
           int i = mapper.delete(shoppingCart);
        if (i <= 0) {
            return new ResultVO(ResultEnum.ERROR);
        }
        return new ResultVO(ResultEnum.SUCCESS);

    }
    @Override
    public int update(ShoppingCart shoppingCart) {
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingCartMapper.class);
        int i = mapper.update(shoppingCart);
        return i;
    }
}
